﻿using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace WebService_Client_Framework
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 静态调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn1_Click(object sender, EventArgs e)
        {
            localhost.WebService1 webService1 = new localhost.WebService1();
            MessageBox.Show(webService1.Sum1(4, 5).ToString());
        }

        /// <summary>
        /// 动态调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn2_Click(object sender, EventArgs e)
        {
            // 读取配置文件，获取配置信息
            string url = ConfigurationManager.AppSettings["WebServiceAddress"];
            string className = ConfigurationManager.AppSettings["ClassName"];
            string methodName = ConfigurationManager.AppSettings["MethodName"];
            string filePath = ConfigurationManager.AppSettings["FilePath"];
            // 调用WebServiceHelper
            WebServiceHelper.CreateWebServiceDLL(url, className, methodName, filePath);
            // 读取dll内容
            byte[] filedata = File.ReadAllBytes(filePath + className + "_" + methodName + ".dll");
            // 加载程序集信息
            Assembly asm = Assembly.Load(filedata);
            Type t = asm.GetType(className);
            // 创建实例
            object o = Activator.CreateInstance(t);
            MethodInfo method = t.GetMethod(methodName);
            // 参数
            object[] args = { 1,2};
            // 调用访问，获取方法返回值
            string value = method.Invoke(o, args).ToString();
            //输出返回值
            MessageBox.Show($"返回值:{value}");
        }

   
    }
}
